package com.mobileares.midp.widgets.client.tab;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-7-21
 * Time: 10:12:50
 * To change this template use File | Settings | File Templates.
 */
public class VerticalTabPanel extends Composite implements HasSelectionHandlers<Integer> {
    private FlowPanel container;
    private FlowPanel tabBar;
    private SimplePanel main;
    private VTabItem selected;
//    private int height;

    public VerticalTabPanel(int width, int height) {
        container = new FlowPanel();
        container.setStyleName("u2-vtab");
        tabBar = new FlowPanel();
        tabBar.setStyleName("u2-vtab-bar");
        main = new SimplePanel();
        main.setStyleName("u2-vtab-main");

        container.add(tabBar);
        container.add(main);
        initWidget(container);

        SimplePanel clear = new SimplePanel();
        clear.setStyleName("clear");
        container.add(clear);
        main.setWidth(width - 31 + "px");
        setHeight(height);

    }

    public void setHeight(int height) {
        tabBar.setHeight(height - 6 + "px");
        main.setHeight(height + "px");
    }

    public void addTabItem(String text, Widget widget) {
        final VTabItem item = new VTabItem(text, widget);
        item.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                int index = tabBar.getWidgetIndex(item);
                setItem(item, index);
            }
        });
        tabBar.add(item);
    }

    public void setTabItem(int index) {
        VTabItem item = getTableItem(index);
        setItem(item, index);
    }

    private void setItem(VTabItem item, int index) {
        if (selected != null)
            selected.setStyle(false);
        selected = item;
        selected.setStyle(true);
        SelectionEvent.fire(VerticalTabPanel.this, index);
        main.setWidget(item.getWidget());
    }

    /**
     * Adds a {@link com.google.gwt.event.logical.shared.SelectionEvent} handler.
     *
     * @param handler the handler
     * @return the registration for the event
     */
    public HandlerRegistration addSelectionHandler(SelectionHandler<Integer> handler) {
        return addHandler(handler, SelectionEvent.getType());
    }

    public VTabItem getTableItem(int index) {
        return (VTabItem) tabBar.getWidget(index);
    }


}
